﻿using System;
using System.Data.Linq;
using System.Linq;
using WebUI.DAL;
using System.Collections.Generic;

namespace WebUI.BL.Coupon
{
    public class CouponBL
    {
        protected void clsCouponInsert(tbl_Coupon coupons)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_Coupon coupon = new tbl_Coupon();
                    coupon.CouponName = coupons.CouponName;
                    coupon.CouponDesc = coupons.CouponDesc;
                    coupon.PriceValue = coupons.PriceValue;
                    coupon.PercentValue = coupons.PercentValue;
                    coupon.MinimumUse = coupons.MinimumUse;
                    coupon.ProductCategoryNo = coupons.ProductCategoryNo;
                    coupon.ProductCategoryName = coupons.ProductCategoryName;
                    coupon.ProductCode = coupons.ProductCode;
                    coupon.ProductName = coupons.ProductName;
                    coupon.FromDate = coupons.FromDate;
                    coupon.ToDate = coupons.ToDate;
                    coupon.Dayperiod = coupons.Dayperiod;
                    coupon.CreatedUser = coupons.CreatedUser;
                    coupon.CreatedDate = DateTime.Now;
                    coupon.Status = coupons.Status;

                    adlerContextDb.tbl_Coupons.InsertOnSubmit(coupon);
                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        protected void clsCouponModify(tbl_Coupon coupons)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var coupon = adlerContextDb.tbl_Coupons.Single(d => d.CouponNo == coupons.CouponNo);
                    coupon.CouponName = coupons.CouponName;
                    coupon.CouponDesc = coupons.CouponDesc;
                    coupon.PriceValue = coupons.PriceValue;
                    coupon.PercentValue = coupons.PercentValue;
                    coupon.MinimumUse = coupons.MinimumUse;
                    coupon.ProductCategoryNo = coupons.ProductCategoryNo;
                    coupon.ProductCategoryName = coupons.ProductCategoryName;
                    coupon.ProductCode = coupons.ProductCode;
                    coupon.ProductName = coupons.ProductName;
                    coupon.FromDate = coupons.FromDate;
                    coupon.ToDate = coupons.ToDate;
                    coupon.Dayperiod = coupons.Dayperiod;
                    coupon.Status = coupons.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        protected void clsCouponDelete(tbl_Coupon coupons)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var coupon = adlerContextDb.tbl_Coupons.Single(d => d.CouponNo == coupons.CouponNo);
                    coupon.Status = coupons.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        // Show Coupon.
        public List<MODEL_SP_ADMIN_SEARCH_COUPON> Get_Admin_Coupon(string couponname, string productcategoryno, string productcode, string datefrom, string dateto, string status)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                int index = 1;
                //var query = from n in contextdb.SP_ADMIN_SEARCH_COUPON(couponname, productcategoryno, productcode, datefrom, dateto, status)
                //            select new MODEL_SP_ADMIN_SEARCH_COUPON
                //            {
                //                Index = index++,
                //                CouponNo = n.CouponNo,
                //                CouponName = n.CouponName,
                //                CouponDesc = n.CouponDesc,
                //                PriceValue = n.PriceValue,
                //                PercentValue = n.PercentValue,
                //                MinimumUse = n.MinimumUse,
                //                ProductCategoryNo = n.ProductCategoryNo,
                //                ProductCategoryName = n.ProductCategoryName,
                //                ProductCode = n.ProductCode,
                //                ProductName = n.ProductName,
                //                FromDate = n.FromDate,
                //                ToDate = n.ToDate,
                //                Dayperiod = n.Dayperiod,
                //                CreatedUser = n.CreatedUser,
                //                CreatedDate = n.CreatedDate,
                //                Status = n.Status
                //            };
                //return query.ToList();
                return null;
            }
        }

        public tbl_Coupon GetCouponByCouponno(int couponno)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                return contextdb.tbl_Coupons.Single(d => d.CouponNo == couponno);
            }
        }
    }
}
